import Public from '@/api/public'
import { formatTimes } from '@/libs/tool'
export default {
  data() {
    return {
      // 字典项
      dictPopupShow: false,
      paramsName: 'formParams',
      dictData: {},
      dictType: '',
      dictValue: '',
      // 日期
      datePopupShow: false,
      dateType: '',
      date: new Date(),
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(3000, 0, 1),
      formatter: (type, val) => {
        if (type === 'year') {
          return `${val}年`
        } else if (type === 'month') {
          return `${val}月`
        }
        return `${val}日`
      },
      // 联级
      cascadeData: {
        cityData: [],
        treeData: []
      },
      cascadeType: '',
      cascadeValue: '',
      cascadeTitle: '',
      cascadePopupShow: false
    }
  },
  methods: {
    // 字典项
    getDict(type) {
      Public.Init.init({ type }).then(res => {
        const { data } = res.data
        data.idType = [
          {
            code: '414',
            description: '普通护照'
          },
          {
            code: '111',
            description: '居民身份证'
          }
        ]
        data.ownerIdType = data.idType
        data.legalPersonIdType = data.idType
        data.operatorIdType = data.idType
        this.dictData = data
        console.log('------------->001dictData', this.dictData)
      })
    },
    /**
     * 字典项 - 弹窗
     * @param dictType 需要赋值的key
     * @param paramsName key所属对象
     */
    dictOpenPopup(dictType, paramsName = 'formParams') {
      this.paramsName = paramsName
      this.dictType = dictType
      this.dictValue = this[this.paramsName][this.dictType]
      this.dictPopupShow = true
    },
    // 字典项 - 弹窗确定
    dictOnConfirm(item) {
      this[this.paramsName][this.dictType] = item.code
      this[this.paramsName][`${this.dictType}Name`] = item.description
      this.dictPopupShow = false
    },
    /**
     * 日期 - 弹窗
     * @param dateType 需要赋值的key
     * @param paramsName key所属对象
     */
    dateOpenPopup(dateType, paramsName = 'formParams') {
      this.paramsName = paramsName
      this.dateType = dateType
      this.date = this[this.paramsName][this.dateType] ? new Date(this[this.paramsName][this.dateType]) : new Date()
      this.datePopupShow = true
    },
    // 日期 - 日期确定
    dateOnConfirm(date) {
      this[this.paramsName][this.dateType] = formatTimes.formatDate(date)
      this.datePopupShow = false
    },
    /**
     * 联级 - 弹窗
     * @param cascadeType 需要赋值的key
     * @param cascadeTitle 显示标题
     * @param paramsName key所属对象
     */
    cascadeOpenPopup(cascadeType, cascadeTitle, paramsName = 'formParams') {
      this.paramsName = paramsName
      this.cascadeTitle = '选择' + cascadeTitle
      this.cascadeType = cascadeType
      this.cascadeValue = this[this.paramsName][this.cascadeType]
      this.cascadePopupShow = true
    },
    // 联级 - 完成
    cascadeOnFinish({ value, selectedOptions }) {
      this[this.paramsName][this.cascadeType] = value
      this[this.paramsName][`${this.cascadeType}Code`] = selectedOptions.map(o => o.value).join(',')
      this[this.paramsName][`${this.cascadeType}Name`] = selectedOptions.map(o => o.title || o.label).join(' ')
      this.cascadePopupShow = false
    }
  }
}
